﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class ContasPagarUC
    Dim objBLL As New ContasPagarBLL
    Dim sta As Integer = 0
#Region "Carrega Grid"
    Sub CarregaGrid()
        DGVDados.DataSource = objBLL.buscar("", 5)
        ' Me.DGVDados.CurrentRow.Selected = False
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs)
        If (PNFiltros.Visible = True) Then
            PNFiltros.Visible = False
        Else
            PNFiltros.Visible = True
            TSBLocalizar.Checked = True
        End If
    End Sub
#End Region
#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        objBLL.limpar()
        Dispose()
    End Sub
#End Region
#Region "Key Press"
    Private Sub CBConsultas_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = True
    End Sub
#End Region
#Region "KeyPress"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged

        If CBFiltro.SelectedIndex = 3 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            TXTLocalizar.Focus()
            CarregaGrid()
        End If
        If CBFiltro.SelectedIndex = 1 Then
            TXTLocalizar.Visible = False
            DTPInicial.Visible = True
            DTPFinal.Visible = True
            LBInformacao.Visible = True
            BBuscar.Visible = True
        Else
            BBuscar.Visible = False
            DTPInicial.Visible = False
            DTPFinal.Visible = False
            LBInformacao.Visible = False
        End If
        TXTLocalizar.Text = ""
    End Sub
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = objBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region
#Region "Load"
    Private Sub ContasPagarUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CBFiltro.SelectedIndex = 0
        CarregaGrid()
        DGVDados.ReadOnly = True
    End Sub
#End Region
#Region "Novo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            Dim frm As New ContasaPagarFRM 'foi alterado 10/12/2012
            frm.Op = "N"
            TSBNovo.BackColor = Color.Gainsboro
            frm.ShowDialog()
            CarregaGrid()
            TSBNovo.BackColor = Color.Transparent
            frm.Dispose()
            frm = Nothing
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "TSBLocalizar"
    Private Sub TSBLocalizar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        CBFiltro.SelectedValue = 1
        If PNFiltros.Visible = True Then
            PNFiltros.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PNFiltros.Visible = True
            ' TXTLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "TXTLocalizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 1 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Fornec./Prestador"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Data Vencimento"
                BBuscar_Click(Nothing, Nothing)
            Case "Centro Custo"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 8)
        End If
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Buscar"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Try
            DGVDados.DataSource = objBLL.locdata(DTPInicial.Text, DTPFinal.Text)
            DGVDados.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "Pagamento"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBPagamento.Click
        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ctp_codigo") Is Nothing) Then
                    indice = linha.Index
                    Dim ind2 As Integer = 1
                    If (Not linha.Cells("icp_codigo") Is Nothing) Then
                        ind2 = linha.Index
                    End If
                End If
            Next
            If (indice <> -1) Then
                sta = Val(DGVDados.Rows(indice).Cells("sta_codigo").Value)
                If sta <> 6 Then
                    If DGVDados.Rows.Count > 0 Then
                        Dim frm As New PagamentoFRM
                        frm.Codigo = Val(DGVDados.Rows(indice).Cells("ctp_codigo").Value)
                        frm.Codigoit = Val(DGVDados.Rows(indice).Cells("icp_codigo").Value)
                        frm.dtemissao = (DGVDados.Rows(indice).Cells("ctp_dtcompra").Value)
                        frm.Op = "P"
                        TSBPagamento.BackColor = Color.Gainsboro
                        frm.ShowDialog()
                        CarregaGrid()
                        frm.Dispose()
                        frm = Nothing
                    Else
                        MsgBox("Nenhuma linha encontrada!")
                    End If
                Else
                    MsgBox("Parcela já paga!", MsgBoxStyle.Exclamation)

                End If
            Else
                MessageBox.Show("Não existe registro para seleção!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            '   MessageBox.Show(ex.Message)
        Finally
            TSBPagamento.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Dim indice As Integer = -1
        Dim frm As New EditaParcelamentoFRM
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("sta_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                sta = Val(DGVDados.Rows(indice).Cells("sta_codigo").Value)
                If sta <> 6 Then
                    frm.Op = "U"
                    Dim Informacao As String
                    If DGVDados.Rows.Count > 0 Then
                        Informacao = DGVDados.CurrentRow.Cells("ctp_codigo").Value.ToString()
                        frm.codctp = Informacao
                        frm.ShowDialog()
                        frm.Dispose()
                        frm = Nothing
                        CarregaGrid()
                    End If
                Else
                    MsgBox("Parcela já paga!", MsgBoxStyle.Exclamation)

                End If
            Else
                MessageBox.Show("Selecione a conta a ser alterada", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch
        End Try
    End Sub
    Private Sub DGVDados_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If DGVDados.Rows.Count > 0 Then
            If e.KeyChar = Convert.ToChar(Keys.Return) Then
                TSBAlterar_Click(Nothing, Nothing)
            End If
        Else
            MsgBox("Não existem dados selecionados!", MsgBoxStyle.Exclamation)
        End If

    End Sub

    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Dim indice As Integer = -1

        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("sta_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                sta = Val(DGVDados.Rows(indice).Cells("sta_codigo").Value)
                If sta <> 6 Then
                    If DGVDados.Rows.Count > 0 Then
                        TSBAlterar_Click(Nothing, Nothing)
                    Else
                        MsgBox("Nenhuma linha encontrada!")
                    End If
                Else
                    MsgBox("Parcela já paga!", MsgBoxStyle.Exclamation)

                End If
            Else
                MessageBox.Show("Selecione a conta a ser alterada", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch
        End Try
           
    End Sub
#Region "Visualizar"
    Private Sub TSBVisualizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVisualizar.Click
        Dim indice As Integer = -1
        Dim frm As New VisualizarFRM
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("sta_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                Dim Informacao As String
                If DGVDados.Rows.Count > 0 Then
                    Informacao = DGVDados.CurrentRow.Cells("ctp_codigo").Value.ToString()
                    frm.cod = Informacao
                    frm.ShowDialog()
                    frm.Dispose()
                    frm = Nothing
                End If
            Else
                MessageBox.Show("Sem registro para seleção", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch
        End Try
    End Sub
#End Region
#Region "Row Pre Paint"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        '  Me.DGVDados.CurrentRow.Selected = False 'tira a seleção da primeira linha da dgvdados
        Try
            If DGVDados.Rows.Count > 0 Then
                If (DGVDados.Rows(e.RowIndex).Cells(15).Value = "Aberto") And (DGVDados.Rows(e.RowIndex).Cells(6).Value = Date.Now.ToShortDateString) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Yellow 'hoje
                End If
                If (DGVDados.Rows(e.RowIndex).Cells(15).Value = "Aberto") And (DGVDados.Rows(e.RowIndex).Cells(6).Value > Date.Now.ToShortDateString) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White 'não vencidas (futuro)
                End If
                If (DGVDados.Rows(e.RowIndex).Cells(15).Value = "Aberto") And (DGVDados.Rows(e.RowIndex).Cells(6).Value < Date.Now.ToShortDateString) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Salmon 'vencidos
                End If
                If (DGVDados.Rows(e.RowIndex).Cells(15).Value = "Fechado") And (DGVDados.Rows(e.RowIndex).Cells(16).Value = 6) Then
                    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.PaleGreen 'pagos
                End If
                'Else
                '    DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
            End If
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "Detalhes"
    Public Sub gerarelatrio(ByVal titulo As String, ByVal formula As String)
        Try
            Dim frm As New RelatoriosfinFRM
            Dim rpt As New ContasPagarDetalhadoREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frm.CrystalReportViewer2.ShowGroupTreeButton = False
                frm.CrystalReportViewer2.ReportSource = rpt
                frm.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frm.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region
#Region "Detalhes"
    Private Sub TSBDetalhes_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBDetalhes.Click
        Dim tab As DataTable
        Dim forn As String
        Try
            Dim objitBLL As New ItensCtasPagarBLL
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("icp_codigo") Is Nothing) Then
                    objitBLL = New ItensCtasPagarBLL(Val(DGVDados.Rows(linha.Index).Cells("icp_codigo").Value.ToString()), 0)

                End If
            Next
            If (Not IsNothing(objitBLL)) Then
                tab = objBLL.localizacod(objitBLL.ctpcodigo)
                forn = tab.Rows(0)("ctp_fornprestador").ToString
                If (objitBLL.icpcodigo > 0) Then gerarelatrio(forn, "{Ofi_ItensContasPagar.icp_codigo}=" & objitBLL.icpcodigo)
            End If
        Catch ex As Exception

        End Try
    End Sub
#End Region
End Class
